<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 350px;
            height: 300px;
            background-color: burlywood;
            margin: 150px auto;
            overflow: auto;
        }
        .btn{
            margin: 10px 0 0 10px;
        }
        .sbox{
            width: 250px;
            border: 1px solid black;
            height: 40px;
            margin: auto auto;
            margin-top: 177px;
        }
        .progress_bar{
            background-color: violet;
            width: 0;
            height: 100%;
            text-align: center;
            line-height: 40px;
        }
    </style>
</head>
<body>
    <div class="box">
        <button class="btn">点我</button>
        <div class="sbox">
            <div class="progress_bar"></div>
        </div>
    </div>
    <script>
        // 10. 模拟进度条
        // - 配合计时器
        const box = document.querySelector('.box')
        const sbox = document.querySelector('.sbox')
        const progress_bar = document.querySelector('.progress_bar')
        let btn = document.querySelector('.btn')
        const percentage = '%'
        let num = 0
        let itv
        btn.onclick = () => {
            num = 0
            clearInterval(itv)
            itv = setInterval(() => {
                num ++
                progress_bar.style.width = num + percentage
                if(num >= 20 && num < 100){
                    progress_bar.innerText = num + percentage
                }else if(num >= 100){
                    progress_bar.innerText = num + percentage
                    clearInterval(itv)
                }else{
                    progress_bar.innerText = ''
                }
            },16)
        }
    </script>
</body>
</html>